package com.zc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.dao.OrderMapper;
import com.zc.entities.Order;
import com.zc.service.AccountService;
import com.zc.service.OrderService;
import com.zc.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:
 * @author: cong
 * @create: 2024/11/4
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private AccountService accountService;

    @Resource
    private StorageService storageService;

    @Override
    public void create(Order order) {
        // 创建订单
        log.info("------->开始创建订单");
        baseMapper.create(order);
        log.info("------->新建订单完成");

        // 扣减库存
        log.info("------->开始扣减库存");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("------->扣减库存完成");

        // 扣减账户余额
        log.info("------->开始扣减余额");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("------->扣减余额完成");

        // 修改订单状态
        log.info("------->开始修改订单状态");
        baseMapper.update(order.getUserId(), 0);
        log.info("------->修改订单状态完成");

        log.info("------->下单完成");
    }
}
